/**
 * Message.cpp - The official Lithne library.
 *
 * Copyright (c) 2011-2012
 * Serge Offermans, Remco Magielse
 * {s.a.m.offermans, r.magielse}@tue.nl
 * Intelligent Lighting Institute (ILI), TU/e.
 *
 * All rights reserved. LAST UPDATE: 01-04-2012
**/

/* The following code makes the Library compatible with Arduino 1.0 */
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#include "Message.h"
#include "XBee.h"
#include "Node.h"

//#define UNKNOWN_ADD16 0xFFFE
//#define NO_SCOPE 1
#include "LithneDefinitions.h"

/* Should we make a constructor where you can set the maximum number of arguments
Message::Message( uint8_t max_args ){}
*/
/** Default Message constructor */
Message::Message()
{
	for(int i;i<MAX_ARGS;i++)
	{
		args[i]=0;
	}
	numArgs 	= 	0;
	function	= 	0;
	scope		=	NO_SCOPE;

	//[IS] set all addresses to zero, otherwise it is undefined what they are initialized to
	//What happens if get is called before set?
	sender16	=	UNKNOWN_16B;
	sender64	= 	XBeeAddress64( 0, UNKNOWN_64B );
	recipient16 = 	UNKNOWN_16B;
	recipient64 = 	XBeeAddress64( 0, UNKNOWN_64B );
}

Message::~Message()
{

}

/* 	
__   _____ ___ ___  
\ \ / / _ \_ _|   \ 
 \ V / (_) | || |) |
  \_/ \___/___|___/ 
                    
*/

/** Set the recipient for the message using 64 bit Address **/
void Message::setRecipient64( XBeeAddress64 _addr64 )
{
	recipient64	=	_addr64;
}
void Message::setRecipient16( uint16_t _addr16 )
{
	recipient16	=	_addr16;
}

/** Clear all message arguments (set them to 0) **/
void Message::clearArguments()
{
	for(int i=0; i<MAX_ARGS; i++)
	{
		args[i] = 0;
	}
	numArgs	=	0;
}
/** Set the function the message should call **/
void Message::setFunction( uint16_t _func )
{
	function	=	_func;
}
/** Store the 64 bit address of the sender of the message **/
void Message::setSender64( XBeeAddress64 _addr64 )
{
	sender64	=	_addr64;
}
/**	Store the 16-bit address of the sender	**/
void Message::setSender16( uint16_t _addr16 )
{
	sender16	=	_addr16;
}
/**	Set both the 16-bit and 64-bit address of the sender in one go	**/
void Message::setSender( uint16_t _addr16, XBeeAddress64 _addr64 )
{
	sender16	=	_addr16;
	sender64	=	_addr64;
}
/**	Sets the scope for this message.	**/
void Message::setScope( uint16_t _scope )
{
	scope	=	_scope;
	/*	Here we can add a line that sets the recipient
		to the BROADCAST address if no address has been
		set yet. This will make sure the message can
		be delivered.	*/
}

/*
 ___  ___   ___  _    ___   _   _  _ 
| _ )/ _ \ / _ \| |  | __| /_\ | \| |
| _ \ (_) | (_) | |__| _| / _ \| .` |
|___/\___/ \___/|____|___/_/ \_\_|\_|

*/

/** Add an argument to the list of arguments **/
bool Message::addArgument( uint16_t arg )
{
	if( numArgs < MAX_ARGS )
	{
		args[ numArgs ] = arg;
		numArgs++;
		return true;
	}
	else
	{
		return false;
	}
}
/* How to add multiple arguments to the array? Discuss this with Elco
boolean addArg( uint16_t* args ) //Add an array of arguments to the array
{

}
*/

/*
 ___ _  _ _____ ___ ___ ___ ___ 
|_ _| \| |_   _| __/ __| __| _ \
 | || .` | | | | _| (_ | _||   /
|___|_|\_| |_| |___\___|___|_|_\
                                
*/

/** Return the number of arguments **/
uint8_t Message::getNumberOfArguments()
{
	return numArgs;
}
/** return the argument at the specified position **/
uint16_t Message::getArgument( uint8_t position )
{
	position = constrain( position, 0, MAX_ARGS );

	return	args[ position ];
}
/** Return the function of the message **/
uint16_t Message::getFunction()
{
	return function;
}
/** return the 16 bit address of the recipient **/
uint16_t Message::getRecipient16()
{
	return recipient16;
}
/** Returns the 16 bit address of the sender **/
uint16_t Message::getSender16()
{
	return sender16;
}
/** Returns the scope of the message **/
uint16_t Message::getScope()
{
	return scope;
}

/*
__  _____            _      _    _                 __ _ _  
\ \/ / _ ) ___ ___  /_\  __| |__| |_ _ ___ ______ / /| | | 
 >  <| _ \/ -_) -_)/ _ \/ _` / _` | '_/ -_|_-<_-</ _ \_  _|
/_/\_\___/\___\___/_/ \_\__,_\__,_|_| \___/__/__/\___/ |_| 

*/

/** Return the receiving XBee address **/
XBeeAddress64 Message::getRecipient64()
{
	return recipient64;
}
/** Return the transmitting XBee address **/
XBeeAddress64 Message::getSender64( )
{
	return sender64;
}
